package gui;

import engine.Attribute;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * This class implements a slider that is associated with an attribute (of a
 * specific Group instance).
 * 
 * It is constructed with min, max and current values from the attribute and
 * changes the attribute's current value when the slider's value is changed by
 * the user.
 * 
 * @author jianweigan
 * 
 */
@SuppressWarnings("serial")
public class AttributeSlider extends RangeSlider implements ChangeListener {

	private Attribute myAttribute;

	/**
	 * Constructs a new AttributeSlider Takes an Attribute object, and creates a
	 * slider using the superclass constructor and the name, min value, and max
	 * value as parameters for the slider. Sets the value of the slider to be
	 * the Attribute's current value, and stores the Attribute as a private
	 * variable.
	 * 
	 * @param attr
	 */
	public AttributeSlider(Attribute attr) {
		super(attr.getName(), attr.getMinValue(), attr.getMaxValue());
		setValue(attr.getCurrentValue());
		myAttribute = attr;
		addChangeListener(this);
	}

	/**
	 * Called when RangeSlider's value changed by the user. Updates the
	 * attribute this slider is associated with.
	 */
	public void stateChanged(ChangeEvent event) {
		if (!getValueIsAdjusting()) {
			myAttribute.setCurrentValue(getValue());
		}
	}

}
